// pages/navigation/index.js
import {
  _get,
  update
} from '../../api/collection_record'
import {
  isDone
} from '../../utils/valid'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    items: [
      // {
      //   name: '坯体原料',
      //   path: '../new-data/index',
      // },
      {
        name: '1-坯体球磨',
        path: '../embryo-powder-spraying/index',
        key: 'ballMillingId',
        params: 1,
        inneed: true
      },
      {
        name: '2-坯体喷粉',
        path: '../dusting/index',
        key: 'dustingId',
        params: 1,
        inneed: true
      },
      {
        name: '3-坯体成型',
        path: '../molding/index',
        key: 'formingId',
        params: 1,
        inneed: true
      },

      {
        name: '4-坯体干燥',
        path: '../dry/index',
        key: 'dryId',
        params: 1,
        inneed: true
      },
      {
        name: '5-釉料1',
        path: '../glaze/index',
        key: 'glazeId',
        params: 1,
        inneed: true
      },
      {
        name: '6-印花',
        path: '../printing/index',
        key: 'printingMethodId',
        params: 1,
        inneed: true
      },
      {
        name: '7-釉料2',
        path: '../glaze/index',
        key: 'glazeIdTwo',
        params: 1,
        inneed: true
      },
      {
        name: '8-烧成',
        path: '../firing/index',
        key: 'firingId',
        params: 1,
        inneed: true
      },
      {
        name: '9-成品',
        path: '../product/index',
        key: 'finishProductId',
        params: 1,
        inneed: true
      }
    ],
    dataSource: [],
    status: {},
    canModify: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    wx.setNavigationBarTitle({
      title: '采集流程导航'
    })
    this.setData({
      id: options.id
    })
    await this.fetchData(options.id);
    this.getStatus();
  },
  async fetchData(id = this.data.id) {
    return new Promise((resolve) => {
      _get(id, res => {
        const {
          items
        } = this.data;
        const dataSource = items.map(it => {
          return {
            ...it,
            inneed: res[it.key] !== -1
          }
        })
        resolve(res);
        this.setData({
          status: res,
          items: dataSource

        })
      })
    })
  },
  async onClickHandle(event) {
 
    const {
      path,
      key
    } = event.currentTarget.dataset.value;
    const index = event.currentTarget.dataset.index;
    if (index > 0) {
      let valid, item;
      for (let i = 0; i < index; i++) {
        valid = await isDone(this.data.id, this.data.items[i]['key'], this.data.status);
        item = this.data.items[i];
        if (!valid) break
      }
      if (!valid) {
        wx.showModal({
          showCancel: false,
          title: '提示',
          content: `请先完成${item.name}`
        })
        return
      }
    }
    if (key === 'glazeIdTwo') {
      wx.navigateTo({
        url: `${path}?id=${this.data.id}&next=firing&sortNumber=1&pageTitle=胚体釉料2&navigation=1`,
      })
    } else {
      wx.navigateTo({
        url: `${path}?id=${this.data.id}&navigation=1&data_id=${this.data.status[key]}`,
      })
    }
  },
  onRecover(event) {
    wx.showModal({
      content: '是否恢复此环节？',
      success: res => {
        if (res.confirm) {
          let item = event.currentTarget.dataset.value;
          update({
            ...this.data.status,
            [item]: null
          }, () => {
            this.fetchData(this.data.id);
          })
        }
      }
    })
  },
 getStatus() {
    const root = wx.getStorageSync('root'),
      {
        status
      } = this.data.status;
 
    const valid = root ? status !== 1 && status !==2 : status === 0; //判断当前记录的状态是否支持修改
    app.globalData.editState = valid;
    this.setData({
      canModify: valid
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.fetchData(this.data.id)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})